<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection"
                    v-if="false">批量删除</el-button>
                <el-select v-model="query.exam_type" placeholder="考试类型" class="handle-select mr10" @change="examSearchFunc1">
                    <el-option key="1" label="完成公开" value="完成公开"></el-option>
                    <el-option key="2" label="不公开" value="不公开"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="考试名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="examSearchFunc2">搜索</el-button>
            </div>
            <!-- 考试列表 -->
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <!-- <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> -->
                <el-table-column prop="exam_name" label="考试名称"></el-table-column>
                <el-table-column prop="exam_type" label="考试类型"></el-table-column>
                <el-table-column prop="exam_limit" label="考试时间"></el-table-column>
                <el-table-column prop="exam_time" label="考试时长"></el-table-column>
                <el-table-column prop="exam_fullcredit" label="考试总分"></el-table-column>
                <el-table-column prop="exam_passline" label="及格线"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit"
                            @click="onlineExamFunc(scope.$index, scope.row)">去考试</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="margin-top:20px;">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="currentPage" :page-sizes="[5, 10, 15, 20]" :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper" :total="pageTotal">
                </el-pagination>
            </div>

        </div>
    </div>
</template>

<script>
// 导入请求数据的接口
import { examList , examSearch , examTotal } from '../../api/index';
export default {
    name: 'onlineexam',
    data() {
        return {
            query: {
                exam_type: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            currentPage: 1,
        };
    },
    created() {
        this.getData();
    },
    methods: {
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.getData(val);
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.getData(val);
        },
        // 获取考试列表
        async getData(page_num=1,page_size=5) {
            // 获取考试列表
            await examList({page_num,page_size}).then(content=>{
                let {code ,msg , result} = content;
                if(code == 200){
                    this.tableData = result;
                }
            })
            .catch(
                err=> console.log(err)
            )
        },
        // 统计考试记录
        async examTotalFunc(){
            examTotal({}).then(content=>{
                let {code ,msg , total} = content;
                if(code == 200){
                    this.pageTotal = total;
                }
            })
            .catch(
                err=> console.log(err)
            )
        },

        // 获取搜索考试内类型
        async examSearchFunc1(){
            let keyword = this.query.exam_type;
            console.log('1.0',{keyword})
            examSearch({keyword}).then(content=>{
                let {code ,msg , result} = content;
                console.log(result,code)
                if(code == 200){
                   
                    this.tableData = result;
                    this.pageTotal = result.length;
                }
            })
            .catch(
                err=> console.log(err)
            )
        },
        // 获取搜索考试名称
        async examSearchFunc2(){
            let keyword = this.query.name;
            // console.log({keyword})
            if(keyword.length == 0){
                this.getData();
                this.examTotalFunc();
                return ;
            }
            examSearch({keyword}).then(content=>{
                let {code ,msg , result} = content;
                if(code == 200){
                    this.tableData = result;
                    this.pageTotal = result.length;
                }
            })
            .catch(
                err=> console.log(err)
            )
        },

        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        onlineExamFunc(index, row) {
            this.idx = index;
            this.form = row;
            // console.log(index, row)
            let {exam_id,topic_lib_id} = row;
            this.editVisible = true;
            this.$router.push({
                path: '/readyexam',
                query: {exam_id,topic_lib_id}
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData(val);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
